/* eslint comma-dangle: "off" */
const fs = require('fs')
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin')
// const CopyWebpackPlugin = require('copy-webpack-plugin')
const CompileEventsPlugin = require('./build/compile-events-plugin')
const filterRemove = require('./build/filter-remove')
const ScriptPlugin = require('./build/script-plugin')
const merge = require('webpack-merge')
const getProdConf = require('./build/webpack.prod')

// 命令行参数
const argv = require('yargs').argv
// const isTest = argv['test'] === true // 有时候可能需要测试编译结果

function isExist (path) {
  let exist = true
  try {
    fs.accessSync(path)
  } catch (err) {
    // 文件和目录不存在
    if (err.code === 'ENOENT') {
      exist = false
    }
  }
  return exist
}

let prodConf
let outputPath
if (argv['img']) { // 后端用来生成预览图用
  outputPath = path.resolve('D:/_work/ccard.shendupeiban.com/src/ccard.shendupeiban.com/ccard.shendupeiban.com/Content/school-culture/html')
  if (!isExist(outputPath)) {
    outputPath = path.resolve(__dirname, 'dist/html')
  }
  baseProdConf = getProdConf({
    // splitCss: true, // css 拆分
    sourceMap: false, // 默认为 true
    // 更改入口 index template
    indexTemplate () {
      return new HtmlWebpackPlugin({
        filename: './index.html',
        template: './src/index.html',
        // chunks: ['main'],
        inlineSource: /(styles\.bundle\.js|main\.js)/,
        minify: {
          removeComments: true,
          collapseWhitespace: true,
          removeAttributeQuotes: true,
          // 内嵌 css js 压缩, 结合 HtmlWebpackInlineSourcePlugin 可能会压缩2次，非必要还是不要设了
          // minifyCSS: true,
          // minifyJS: true
          // more options:
          // https://github.com/kangax/html-minifier#options-quick-reference
        }
      })
    }
  })
  baseProdConf.entry.main = './src/main-img/main.js'
  prodConf = {
    output: {
      path: outputPath,
      filename: '[name].js?_=[chunkhash:7]',
      chunkFilename: '[name].bundle.js?_=[chunkhash:7]',
      // publicPath: '/Content/school-culture/'
    },
    plugins: [
      // 编译事件
      new CompileEventsPlugin({
        compile () {
          // 编译前删除
        },
        done () {
          // 编译后删除
          filterRemove(outputPath, /\.(js)$/)
          // filterRemove(outputPath + '/static', /\.(js|css)$/)
        }
      }),
      // 将抽离的 css、js 包含进 html 文件
      new HtmlWebpackInlineSourcePlugin(),
    ]
  }
} else {
  outputPath = path.resolve('D:/_work/ccard.shendupeiban.com/src/ccard.shendupeiban.com/ccard.shendupeiban.com/Content/school-culture')
  if (!isExist(outputPath)) {
    outputPath = path.resolve(__dirname, 'dist')
  }
  baseProdConf = getProdConf({
    // splitCss: true, // css 拆分
    sourceMap: false, // 默认为 true
  })

  prodConf = {
    // 外网调试
    // watch: true,
    // devtool: 'source-map',

    // 不打包的模块
    // 键为 import 调用名，值为全局名称
    externals: {
      'vue': 'Vue'
      // 'vue-router': 'VueRouter'
    },
    output: {
      path: outputPath,
      filename: '[name].js?_=[chunkhash:7]',
      chunkFilename: '[name].bundle.js?_=[chunkhash:7]',
      // publicPath: '/Content/school-culture/'
    },
    plugins: [
      // 编译事件
      new CompileEventsPlugin({
        compile () {
          // 编译前删除
          filterRemove(outputPath, /\.js$|\.map$|^index.html$/)
        },
        done () {
          // 编译后删除
          // filterRemove(outputPath, /^index.html$/)
        }
      }),
      // 添加指定的 cdn 包。或者指定路径的包也行
      new ScriptPlugin([
        'http://style.shenduxuetang.com/vue/2.5.22/vue.min.js',
        // 配合 copy-webpack-plugin 使用
        // 'js/vue-router.min.js',
      ]),
      // 打包分析
      new (require('webpack-bundle-analyzer').BundleAnalyzerPlugin)({
        analyzerMode: 'static',
        reportFilename: './report.html',
        openAnalyzer: false // 自动打开
      })
    ]
  }
}

webpack(merge(baseProdConf, prodConf), require('./build/msg-webpack'))
